package com.wbs.rest.resource;

import com.fasterxml.jackson.annotation.JsonUnwrapped;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.ResourceSupport;
import org.springframework.util.Assert;

import javax.xml.bind.annotation.XmlAnyElement;
import java.util.Arrays;

public class RestSqlResource<T> extends ResourceSupport {

    private final T content;

    public RestSqlResource(T content){
        this.content = content;
    }

    public RestSqlResource(T content, Link... links) {
        this(content, (Iterable) Arrays.asList(links));
    }

    public RestSqlResource(T content, Iterable<Link> links) {
        Assert.notNull(content, "Content must not be null!");
//        Assert.isTrue(!(content instanceof Collection), "Content must not be a collection! Use Resources instead!");
        this.content = content;
        this.add(links);
    }

    @JsonUnwrapped
    @XmlAnyElement
    public T getContent() {
        return this.content;
    }


    @Override
    public String toString() {
        return String.format("Resource { content: %s, %s }", this.getContent(), super.toString());
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        } else if (obj != null && obj.getClass().equals(this.getClass())) {
            RestSqlResource<?> that = (RestSqlResource)obj;
            boolean contentEqual = this.content == null ? that.content == null : this.content.equals(that.content);
            return contentEqual ? super.equals(obj) : false;
        } else {
            return false;
        }
    }

    @Override
    public int hashCode() {
        int result = super.hashCode();
        result += this.content == null ? 0 : 17 * this.content.hashCode();
        return result;
    }
}
